<?php
$caption = BUtils::T('overview_lbl');
$this->pageTitle = $caption;

$this->breadcrumbs = array(
	$caption,
);

//running days
$differ = time() - Yii::app()->user->start_ts;
$days_string = sprintf('%d', round($differ / 3600 / 24));

//stats calculate
$reach = Yii::app()->locale->numberFormatter->formatDecimal($offers['reach']);
$notifications = Yii::app()->locale->numberFormatter->formatDecimal($offers['notifications']);
$clicks = Yii::app()->locale->numberFormatter->formatDecimal($offers['clicks']);
$redeems = Yii::app()->locale->numberFormatter->formatDecimal($offers['redeems']);

//current location
$latitude = Yii::app()->user->getState('latitude');
$longitude = Yii::app()->user->getState('longitude');
if (!empty($latitude) and !empty($longitude)) {
	$location_js_array = "[{$latitude}, {$longitude}]";
} else {
	$location_js_array = 'null';
}
?>
<div class="page-header position-relative">
	<h1><?php echo $caption; ?></h1>
</div><!--/.page-header-->

<div class="row-fluid">
	<div class="row-fluid">
		<div class="span4">
			<div class="alert alert-block alert-info">
				<button type="button" class="close" data-dismiss="alert">
					<i class="icon-remove"></i>
				</button>
				<h5><?php echo BUtils::T('welcome_to_shop_desc', array('{shop_name}'=>'Yamii for Business')); ?></h5>
				<p></p>
			</div>
		</div>
		<div class="span4 center">
			<div class="infobox infobox-green bigger">
				<div class="infobox-icon pull-left">
					<i class="icon-group"></i>
				</div>
				<div class="infobox-data">
					<span class="infobox-data-number"><?php echo $redeems; ?></span>
					<div class="infobox-content"><strong><?php echo BUtils::T('total_visits_lbl'); ?></strong></div>
					<div class="hr hr8"></div>
					<span class="infobox-data-number-sub"><?php echo $clicks; ?></span>
					<div class="infobox-content-sub"><?php echo BUtils::T('special_report_clicks_lbl'); ?></div>
				</div>
			</div>
		</div>
		<div class="span4 center">
			<div class="infobox infobox-orange3 bigger">
				<div class="infobox-icon">
					<i class="icon-certificate"></i>
				</div>
				<div class="infobox-data">
					<span class="infobox-data-number"><?php echo $reach; ?></span>
					<div class="infobox-content"><strong><?php echo BUtils::T('total_reach_lbl'); ?></strong></div>
					<div class="infobox-content"></div>
					<div class="hr hr8"></div>
					<span class="infobox-data-number-sub"><?php echo $notifications; ?></span>
					<div class="infobox-content-sub"><?php echo BUtils::T('notifications_lbl'); ?></div>
				</div>
			</div>
		</div>
	</div>
	<div class="space"></div>
	
	<div class="row-fluid">
		<div class="span12">
			<h3 class="row-fluid header lighter">
				<div class="span6">
					<i class="icon-group green"></i> <?php echo BUtils::T('customers_stats_lbl'); ?>
				</div>
				<div class="span6"></div>
			</h3>
			<div class="row-fluid clearfix">
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter"><?php echo BUtils::T('people_nearby_lbl'); ?> <small>(1km)</small></h4>
							<span class="label label-yellow"><?php echo BUtils::T('refresh_time_lbl'); ?>: <i id="refresh-time"></i></span>
							<div class="widget-toolbar">
								<a href="#" id="people-nearby-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
								<a href="#" data-action="collapse">
									<i class="icon-chevron-up"></i>
								</a>
							</div>
						</div>
						<div class="alert alert-info">
							<?php echo BUtils::T('people_nearby_eg'); ?>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="people-nearby">
								<div id="people-nearby-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter">
								<?php echo BUtils::T('customers_recent_lbl'); ?>
							</h4>
							<div class="widget-toolbar">
								<a href="#" id="customers-recent-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
								<a href="#" data-action="collapse">
									<i class="icon-chevron-up"></i>
								</a>
							</div>
						</div>
						<div class="alert alert-info">
							<?php echo BUtils::T('customers_recent_eg'); ?>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="customers-recent">
								<div id="customers-recent-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
			</div>
		</div>
	</div>
	<div class="space"></div>
	
	<div class="row-fluid">
		<div class="span12">
			<h3 class="header lighter">
				<i class="icon-certificate orange"></i> <?php echo BUtils::T('offers_stats_lbl'); ?>
			</h3>
			<div class="row-fluid clearfix">
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter"><?php echo BUtils::T('offers_results_lbl'); ?></h4>
							<div class="widget-toolbar">
								<a href="#" id="offers-stats-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
							</div>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="offers-stats">
								<div id="offers-stats-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
				<div class="span6">
					<div class="widget-box transparent">
						<div class="widget-header widget-header-flat">
							<h4 class="lighter">
								<?php echo BUtils::T('today_offers_lbl'); ?>
								<small>
									(<a href="<?php echo Yii::app()->createUrl('/offer'); ?>"><?php echo BUtils::T('offers_list_lbl'); ?></a>)
								</small>
							</h4>
							<div class="widget-toolbar">
								<a href="#" id="offers-recent-btn" data-action="reload">
									<i class="icon-refresh"></i>
								</a>
							</div>
						</div>
						<div class="widget-body">
							<div class="widget-main" id="offers-recent">
								<div id="offers-recent-error" style="display:none;">
									<?php BHelper::HtmlErrorWell(); ?>
								</div>
							</div>
						</div><!--/widget-body-->
					</div><!--/widget-box-->
				</div>
			</div>
		</div>
	</div>
	
	<div class="space"></div>
</div>

<!-- Modal -->
<div id="location-model" class="modal hide" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3><?php BUtils::T('confirm_location_lbl'); ?></h3>
	</div>
	<div class="modal-body">
		<p><img src="<?php echo Yii::app()->baseUrl; ?>/img/ajax-loaders/ajax-loader-7.gif" /></p>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true"><?php BUtils::T('close_btn'); ?></button>
		<button class="btn btn-primary" id="save-location"><?php BUtils::T('save_changes_btn'); ?></button>
	</div>
</div>
<audio id="soundHandle" style="display: none;"></audio>
<script>
$(function() {
	//load recent customers
	$('#customers-recent-btn').click(function(ev){
		load_recent_customers();
	});
	//load people nearby
	var location = <?php echo $location_js_array; ?>;
	$('#people-nearby-btn').click(function(ev){
		if (location == null) {
			//give a location list for confirmation
			$('#location-model').modal({
				remote: '<?php echo Yii::app()->createUrl('/profile/location'); ?>'
			});
		} else {
			load_people_nearby(location[0], location[1]);
		}
	});
	//load offers stats
	$('#offers-stats-btn').click(function(ev){
		load_offers_stats();
	});
	//load recent offers
	$('#offers-recent-btn').click(function(ev){
		load_recent_offers();
	});
	
	//refresh within 10 mins
	var timer = setInterval(function(){ if(location!=null)$('#people-nearby-btn').trigger('click'); }, 1000*600);
	setTimeout(function(){ $('#people-nearby-btn').trigger('click'); }, 50);
	setTimeout(function(){ $('#customers-recent-btn').trigger('click'); }, 500);
	setTimeout(function(){ $('#offers-recent-btn').trigger('click'); }, 1000);
	setTimeout(function(){ $('#offers-stats-btn').trigger('click'); }, 2000);
	
	//save location function
	$('#save-location').click(function() {
		var location_string = $('#location-model input:checked').val();
		if (location_string && location_string != '') {
			var loc = location_string.split(',');
			var lat = loc[0];
			var lng = loc[1];
			$.ajax({
				'type':'POST', 'url':'<?php echo Yii::app()->createUrl('/profile/savelocation'); ?>', 'data':{lat:lat, lng:lng},
				success:function(response) {
					$('#people-nearby').html(response);
					$('#location-model').modal('hide');
					load_people_nearby(lat, lng);
				}, error: function() {
					alert('Please select your store address!');
				}
			});
		} else {
			alert('Please select your store address!');
		}
	});
});

function load_offers_stats() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/offer/stats'); ?>', 'data':{},
		beforeSend:function(){$('#offers-stats-error').hide();},
		success:function(response) {
			$('#offers-stats').html(response);
		},
		error:function() {
			$('#offers-stats-error').show();
		}
	});
}

function load_recent_offers() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/offer/recent'); ?>', 'data':{},
		beforeSend:function(){$('#offers-recent-error').hide();},
		success:function(response) {
			$('#offers-recent').html(response);
		},
		error:function() {
			$('#offers-recent-error').show();
		}
	});
}

function load_recent_customers() {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/customer/recent'); ?>', 'data':{},
		beforeSend:function(){$('#customers-recent-error').hide();},
		success:function(response) {
			$('#customers-recent').html(response);
		},
		error:function() {
			$('#customers-recent-error').show();
		}
	});
}

function load_people_nearby(lat, lng) {
	$.ajax({
		'type':'GET', 'url':'<?php echo Yii::app()->createUrl('/customer/nearby'); ?>', 'data':{lat:lat, lng:lng},
		beforeSend:function(){$('#people-nearby-error').hide();},
		success:function(response) {
			$('#people-nearby').html(response);
			$('#refresh-time').html(moment().format('h:mm a'));
			var number = $('#people-number').html();
			if (number > 10) {
				play_sound();
			}
		},
		error:function() {
			$('#people-nearby-error').show();
		}
	});
}

function play_sound() {
	var soundHandle = document.getElementById('soundHandle');
	soundHandle.src = '<?php echo Yii::app()->baseUrl; ?>/assets/media/appinvite.wav';
	soundHandle.play();
}
</script>